<template>
  <div style="color: red">编码与解码</div>
  <br />
  <div>1.encodeURI decodeURI</div>
  <div>2.encodeURIComponent decodeURIComponent</div>
  <div>
    区别：encodeURLComponent不仅对中文进行编码，还会对主机名，协议，符号等都做了转换
  </div>
</template>

<script setup lang="ts" name="Decode">
const urlStr = "https://www.google.com?name=张三&age=25";
const res = encodeURI(urlStr);
const res2 = decodeURI(res);
console.log(res);
console.log(res2);

const urlToObj = (url: string) => {
  const obj = {};
  const index = url.indexOf("?");
  const value = url.slice(index + 1);
  value.split("&").map((item) => {
    let [key, value] = item.split("=");
    obj[key] = decodeURI(value);
  });
  return obj;
};

console.log(urlToObj(res));
</script>

<style scoped lang="scss"></style>
